package com.zmj.sy.mom.srv.aps.utils;

import cn.hutool.core.collection.CollUtil;
import com.zmj.sy.mom.srv.aps.bean.vo.lantek.PlatePreNestStockVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportNestPlateNeedResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportNestPlateSumResVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TextureReplaceUtils {

    public static List<PlatePreNestStockVo> plateReplace(List<PlatePreNestStockVo> reqVo, Map<String, String> map) {
        if (CollUtil.isEmpty(reqVo)) {
            return reqVo;
        }
        reqVo.stream().forEach(e->{
            String texture = map.get(e.getTexture());
            if (texture != null) {
                e.setTexture(texture);
                String[] split = e.getStandard().split("δ");
                e.setStandard(texture + "δ" + split[1]);
            }
        });
        return reqVo.stream()
                .collect(Collectors.groupingBy(PlatePreNestStockVo::getStandard))
                .entrySet().stream()
                .map(entry -> {
                    PlatePreNestStockVo vo = new PlatePreNestStockVo();
                    String standard = entry.getKey();
                    int amount = entry.getValue().stream().mapToInt(PlatePreNestStockVo::getAmount).sum();
                    BigDecimal weight = entry.getValue().stream().map(PlatePreNestStockVo::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
                    vo.setStandard(standard);
                    vo.setTexture(entry.getValue().get(0).getTexture());
                    vo.setThickness(entry.getValue().get(0).getThickness());
                    vo.setAmount(amount);
                    vo.setWeight(weight);
                    return vo;
                }).collect(Collectors.toList());
    }

    public static List<ReportNestPlateNeedResVo> needReplace(List<ReportNestPlateNeedResVo> reqVo, Map<String, String> map) {
        if (CollUtil.isEmpty(reqVo)) {
            return reqVo;
        }
        reqVo.stream().forEach(e->{
            String texture = map.get(e.getTexture());
            if (texture != null) {
                e.setTexture(texture);
            }
        });
        return reqVo.stream()
                .collect(Collectors.groupingBy(e->e.getConstructCode() + "δ" + e.getMaterCode() + "δ" + e.getTexture() + "δ" + e.getThickness()))
                .entrySet().stream()
                .map(entry -> {
                    ReportNestPlateNeedResVo vo = new ReportNestPlateNeedResVo();
                    List<ReportNestPlateNeedResVo> list = entry.getValue();

                    List<ReportNestPlateNeedResVo> collect = list.stream().filter(e -> e.getPlateRate() != null).collect(Collectors.toList());

                    if (CollUtil.isNotEmpty(collect)) {
                        vo = MkBeanUtils.copyProperties(collect.get(0), ReportNestPlateNeedResVo.class);
                    } else {
                        vo = MkBeanUtils.copyProperties(list.get(0), ReportNestPlateNeedResVo.class);
                    }

//                    int planAmount = entry.getValue().stream().mapToInt(ReportNestPlateNeedResVo::getPlanAmount).sum();
//                    BigDecimal planPlateWeight = entry.getValue().stream().map(ReportNestPlateNeedResVo::getPlanPlateWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal partWeight = entry.getValue().stream().map(ReportNestPlateNeedResVo::getPartWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
                    String infoMsg = "";
                    if (list.stream().anyMatch(e -> e.getInfoMsg() != null)) {
                        List<ReportNestPlateNeedResVo> collect1 = list.stream().filter(e -> e.getInfoMsg() != null).collect(Collectors.toList());
                        infoMsg = collect1.get(0).getInfoMsg();
                    }
//                    vo.setPlanAmount(planAmount);
//                    vo.setPlanPlateWeight(planPlateWeight);
                    vo.setPartWeight(partWeight);
                    vo.setInfoMsg(infoMsg);
                    return vo;
                }).collect(Collectors.toList());
    }

    public static List<ReportNestPlateSumResVo> sumReplace(List<ReportNestPlateSumResVo> reqVo, Map<String, String> map) {
        if (CollUtil.isEmpty(reqVo)) {
            return reqVo;
        }
        reqVo.stream().forEach(e->{
            String texture = map.get(e.getTexture());
            if (texture != null) {
                e.setTexture(texture);
            }
        });
        return reqVo.stream()
                .collect(Collectors.groupingBy(e->e.getConstructCode() + "δ" + e.getTexture() + "δ" + e.getThickness()))
                .entrySet().stream()
                .map(entry -> {
                    ReportNestPlateSumResVo vo = new ReportNestPlateSumResVo();
                    List<ReportNestPlateSumResVo> list = entry.getValue();

                    List<ReportNestPlateSumResVo> collect = list.stream().filter(e -> e.getPlateRate() != null).collect(Collectors.toList());

                    if (CollUtil.isNotEmpty(collect)) {
                        vo = MkBeanUtils.copyProperties(collect.get(0), ReportNestPlateSumResVo.class);
                    } else {
                        vo = MkBeanUtils.copyProperties(list.get(0), ReportNestPlateSumResVo.class);
                    }

//                    int planAmount = entry.getValue().stream().mapToInt(ReportNestPlateSumResVo::getPlanAmount).sum();
//                    BigDecimal planPlateWeight = entry.getValue().stream().map(ReportNestPlateSumResVo::getPlanPlateWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal partWeight = entry.getValue().stream().map(ReportNestPlateSumResVo::getPartWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
                    String infoMsg = "";
                    if (list.stream().anyMatch(e -> e.getInfoMsg() != null)) {
                        List<ReportNestPlateSumResVo> collect1 = list.stream().filter(e -> e.getInfoMsg() != null).collect(Collectors.toList());
                        infoMsg = collect1.get(0).getInfoMsg();
                    }
//                    vo.setPlanAmount(planAmount);
//                    vo.setPlanPlateWeight(planPlateWeight);
                    vo.setPartWeight(partWeight);
                    vo.setInfoMsg(infoMsg);
                    return vo;
                }).collect(Collectors.toList());
    }

}
